# aa = ['a', 'b', 'c', 'd']
# bb = ['1', '2', '3', '4']
# for i in aa:
#     for j in range(len(aa)):
#         # print('nihao'+j)
#         print(i+'_'+str(j))

# # # print(len(aa))
# # for i in range(len(aa)):
# #     print(i)

from bs4 import BeautifulSoup
import requests


def func(src):
    resp = requests.get(src)
    resp.encoding = 'utf-8'
    page = BeautifulSoup(resp.text, "html.parser")
    return page


# resp = requests.get("https://www.umei.cc/meinvtupian/meinvxiezhen/")
# resp.encoding = 'utf-8'
# # print(resp.text)

# main_page = BeautifulSoup(resp.text, "html.parser")

main_page = func("https://www.umei.cc/meinvtupian/meinvxiezhen/")
# print(main_page)
alst = main_page.find("div", attrs={"class": "TypeList"}).find_all(
    "a", attrs={"class": "TypeBigPics"})
# print(type(alst))
for a in alst:
    for b in range(len(alst)):
        cc = a.get("href").split(".htm")[0]
        dd = cc.split("/")[5]
        # print(dd[5])
        href = cc + "_" + str(b + 2) + "." + "htm"
        print(href)
        # resp1 = requests.get(href)
        # resp1.encoding = 'utf-8'
        # child_page = BeautifulSoup(resp1.text, "html.parser")
        child_page = func(href)
        if child_page.find("div", attrs={"class": "ImageBody"}):
            src = child_page.find(
                "div", attrs={"class": "ImageBody"}).find("img").get("src")
            f = open("tu_%s_%s.jpg" % (dd, b), mode="wb")
            f.write(requests.get(src).content)
        else:
            href = a.get("href")
            # dd = cc.split("/")[5]
            # print(dd[5])
            # href = cc + "_" + str(b + 2) + "." + "htm"
            # print(href)
            # resp1 = requests.get(href)
            # resp1.encoding = 'utf-8'
            # child_page = BeautifulSoup(resp1.text, "html.parser")
            solepage = func(href)
            # if child_page.find("div", attrs={"class": "ImageBody"}):
            src = solepage.find(
                "div", attrs={"class": "ImageBody"}).find("img").get("src")
            f = open("tu_%s_01.jpg" % dd, mode="wb")
            f.write(requests.get(src).content)

            break
            # if src:
            #     f = open("tu_%s.jpg" % b, mode="wb")
            #     f.write(requests.get(src).content)
            # else:
            #     print("找不到")
            # print(a.get("href"))
